/*
 * DictionaryManager.java
 *
 * Created on 15 de octubre de 2006, 1:51
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ub.es.ubictionary.logic;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import ub.es.ubictionary.io.JarManager;
import ub.es.ubictionary.io.Reader;

/**
 *
 * @author aram
 */
public class GlobalManager {
    
    DictionaryManager dictionaryManager;
    KeyboardManager keyboardManager;
    LanguageManager languageManager;
    /** Creates a new instance of DictionaryManager */
    public GlobalManager() {
        extractJar();
        
        dictionaryManager = new DictionaryManager();
        keyboardManager = new KeyboardManager();
        languageManager = new LanguageManager();
        try {
            dictionaryManager.loadDictionaries();
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error: the characterset of the file is not compatible with this program (use UTF8)");
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error: the selected dictionary file can not be founded.");
        } catch (IOException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error: there was an error reading the dictionary file.");
        }
        
        try {
            languageManager.loadLanguages();
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error: the characterset of the file is not compatible with this program (use UTF8)");
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error: the language file can not be found.");
        } catch (IOException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error: there was an error reading the language file.");
        }
        
        try {
            keyboardManager.loadKeyboards();
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error: the characterset of the file is not compatible with this program (use UTF8)");
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error: the keyboard file can not be found.");
        } catch (IOException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error: there was an error reading the keyboard file.");
        }
    }
    
    public void generateJar(){
        JarManager.createJar("Omnidic.jar");
    }
  
    private void extractJar(){
        JarManager.clearDirectory();
        try {
            JarManager.extractJar("Omnidic.jar");
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "There was an error during the extraction of files from JAR");
        } catch (IOException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "There was an error during the extraction of files from JAR");
        }
    }
    
    /*public DictionaryManager getDictionaryManager(){
        return dictionaryManager;
    }
    
    public LanguageManager getLanguageManager(){
        return languageManager;
    }
    
    public KeyboardManager getKeyboardManager(){
        return keyboardManager;
    }*/
    
    public void addDictionary(String file, String name, String separador) throws FileNotFoundException, UnsupportedEncodingException, IOException {
        dictionaryManager.addDictionary(file, name, separador);
    }
    
    public int getCuantityOfDictionaries(){
        return dictionaryManager.getCuantityOfDictionaries();
    }
    
    public boolean deleteDictionary(String name){
        return dictionaryManager.deleteDictionary(name);
    }
    
    public Language openLanguage(String file) throws FileNotFoundException, UnsupportedEncodingException, IOException, InvalidLanguageFileException{
            return languageManager.openLanguage(file);
    }
    
    public Language getLanguage(String name){
        return languageManager.getLanguage(name);
    }
    
    public boolean addLanguage(String name, String [] words){
        return languageManager.addLanguage(name, words);
    }
    
    public int getCuantityOfLanguages(){
        return languageManager.getCuantityOfLanguages();
    }
    
    public boolean deleteLanguage(String name){
        return languageManager.deleteLanguage(name);
    }
    
     public ArrayList getLanguages(){
         return languageManager.getLanguages();
     }
     
     public ArrayList getKeyboards(){
         return keyboardManager.getKeyboards();
     }
     
     public ArrayList getDictionaries(){
         return dictionaryManager.getDictionaries();
     }
     
     public int getCuantityOfKeyboards(){
         return keyboardManager.getCuantityOfKeyboards();
     }
     
     public boolean deleteKeyboard(String name) {
         return keyboardManager.deleteKeyboard(name);
     }
     
     public Keyboard getKeyboard(String name) {
         return keyboardManager.getKeyboard(name);
     }
     
     public boolean addKeyboard(String name, String[] keys) {
         return keyboardManager.addKeyboard(name, keys);
     }
     
     public Keyboard openKeyboard(String file) throws FileNotFoundException, UnsupportedEncodingException, IOException, InvalidKeyboardFileException {
         return keyboardManager.openKeyboard(file);
     }
}
